/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.gui.surveillance;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;

public class AgentInterfaceSurveillance extends GuiAgent
{
	private static final long serialVersionUID = 1L;
	private PropertyChangeSupport changes;
	
	//DF Information
	public static final String DF_TYPE = "AgentInterfaceSurveillance";
	
	//Events
	public static final String UPDATE_DATA = "updateData";
	public static final String NOTIFY_ERROR = "notifyError";
	
	protected void setup()
	{
		//PropertyChangeSupport
		changes = new PropertyChangeSupport(this);

		//Frame
		Object[] args = getArguments();
		new InterfaceSurveillance(this, (Integer)args[0]);
		
		//Register DF
		register();
		
		//Behaviour
		addBehaviour(new AgentInterfaceSurveillanceBhv());
	}
	
	private void register()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(AgentInterfaceSurveillance.DF_TYPE); sd.setName(getName());
		dfd.addServices(sd);
		try { DFService.register(this, dfd); }
		catch (Exception fe) { System.out.println("Error during registration..."); }
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		changes.addPropertyChangeListener(listener);
	}
	
	public void sendEvent(int nbRessourceCollectedRessource)
	{
		changes.firePropertyChange(AgentInterfaceSurveillance.UPDATE_DATA, null, nbRessourceCollectedRessource);
	}
	
	public void sendEvent(String errorMessage)
	{
		changes.firePropertyChange(AgentInterfaceSurveillance.NOTIFY_ERROR, null, errorMessage);
	}
	
	protected void onGuiEvent(GuiEvent arg0)
	{
		//No event
	}
	
	public void takeDown()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());

		try { DFService.deregister(this, dfd); }
		catch (FIPAException e) { }
	}
}
